package com.starbaba.mall.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.base.common.util.StringUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.mall.service.ProductItemService;
import com.starbaba.model.dictionary.Constant;
import com.tuanzi.server.loan_base.model.db.Loan_Mall_Item;
import com.tuanzi.server.loan_base.model.db.Loan_Mall_Item_Topic;

@Component
public class ProductItemServiceImpl implements ProductItemService{
	
	@SuppressWarnings("deprecation")
    private static BaseDao dao = new BaseDao(Constant.POOL_LIFE);
	
	public List<Loan_Mall_Item> getDateList(int iDisplayStart, int iDisplayLength, PageResponse pageResponse, Map<String, Object> paramsMap){
		String startTime = (String) paramsMap.get("startTime");  //开始时间
		String endTime = (String) paramsMap.get("endTime");  //结束时间
		String title = (String) paramsMap.get("title");  //商品名称
		String sourceId = (String) paramsMap.get("sourceId");  //商品ID
		String sourceType = (String) paramsMap.get("sourceType");  //商品来源
		String topicId = (String) paramsMap.get("topicId");  //所属类目
		String status = (String) paramsMap.get("status");  //状态
		String isCouponItem = (String) paramsMap.get("is_coupon_item");  //是否领券商品

		String sql = "SELECT * FROM loan_mall_item i WHERE 1=1 ";
		
		String selectSql = "";
		if (!StringUtils.isEmpty(startTime)) {
            selectSql += " AND i.createtime >= '" + startTime  + "'";
        }
		if (!StringUtils.isEmpty(endTime)) {
			selectSql += " AND i.createtime <= '" +endTime + "'";
		}
		if (!StringUtils.isEmpty(title)) {
            selectSql += " AND i.title like '%" + title  + "%'";
        }
		if (!StringUtils.isEmpty(sourceId)) {
			selectSql += " AND i.source_id = '" +sourceId + "'";
		}
		if (!StringUtils.isEmpty(sourceType)) {
			selectSql += " AND i.source_type = " +sourceType;
		}
		if (!StringUtils.isEmpty(status)) {
			selectSql += " AND i.status = " +status;
		}
		if (!StringUtils.isEmpty(isCouponItem)) {
			selectSql += " AND i.is_coupon_item = " +isCouponItem;
		}

		sql += selectSql;
		sql += " order by i.createtime desc limit " + iDisplayStart + " , " + iDisplayLength;

		String countSql = "select count(1) FROM loan_mall_item i where 1=1 " + selectSql;
		
		List<Loan_Mall_Item> list = null;
		try {
			int count = dao.getInt(countSql);
			list = dao.list(sql, Loan_Mall_Item.class, new Object[] {});
			for(Loan_Mall_Item item: list){
				String user_name = item.getUser_name();
				if(StringUtils.isEmpty(user_name)){
					item.setUser_name("-");
				}
			}
			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		} catch (AppException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Loan_Mall_Item> getLoan_Mall_Item(String source_ids) {
		
		String sql = "SELECT * FROM `loan_mall_item` WHERE source_id in(" + source_ids + ") order by field (source_id, " + source_ids + ")";
		List<Loan_Mall_Item> list = null;
		try {
			list = dao.list(sql, Loan_Mall_Item.class);
		} catch (AppException e) {
			LogService.error(e);
		}
		return list;
	}

	@Override
	public List<Loan_Mall_Item_Topic> getLoan_Mall_ItemList(String source_ids) {
		
		List<Loan_Mall_Item_Topic> list = new ArrayList<Loan_Mall_Item_Topic>();
		
		if(StringUtils.isEmpty(source_ids)){
			return list;
		}
		
		String sql = "SELECT source_id AS item_source_id,title AS item_title,status AS item_status ,is_coupon_item "
				+ " FROM loan_mall_item where source_id in(" + source_ids + ") order by field (source_id, " + source_ids + ")";
		
		try {
			list = dao.list(sql, Loan_Mall_Item_Topic.class);
			for(Loan_Mall_Item_Topic topic: list){  //默认位置为0
				topic.setSort_index(0);
			}
		} catch (AppException e) {
			LogService.error(e);
		}
		return list;
	}

	@Override
	public int countLoan_Mall_ItemList(String source_ids) {
		
		if(StringUtils.isEmpty(source_ids)){
			return 0;
		}
		
		String sql = "SELECT COUNT(*) FROM `loan_mall_item` WHERE source_id IN(" + source_ids + ")";
		int result = 0;
		try {
			result = dao.getInt(sql);
		} catch (AppException e) {
			LogService.error(e);
		}
		return result;
	}
	
	
	@Override
	public Loan_Mall_Item getLoanMallItemBySourceId(String source_id) {
		
		String sql = "select * from `loan_mall_item` where source_id = ?";
		Loan_Mall_Item item = new Loan_Mall_Item();
		try {
			
			item = dao.get(sql, Loan_Mall_Item.class, source_id);
			
		} catch (AppException e) {
			LogService.error(e);
		}
		
		return item;
	}
	

}
